package artid.com.arttogo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.NewsList;

/**
 * 艺术资讯adapter 创建人：潘登 创建时间：2017年2月13日10:24:31
 */
public class ArtNewsAdapter extends BaseAdapter {
    private Context mContext;
    private int length = -1;
    public ArrayList<NewsList> listData;

    public ArtNewsAdapter(Context mContext, ArrayList<NewsList> list) {
        super();
        this.mContext = mContext;
        listData = list;

    }

    public ArtNewsAdapter(Context mContext, ArrayList<NewsList> list, int length) {
        super();
        this.mContext = mContext;
        this.length = length;
        listData = list;

    }

    public void addList(List<NewsList> addlist) {
        listData.addAll(addlist);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder = null;
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = LayoutInflater.from(mContext).inflate(R.layout.item_artnews,
                    null);
            holder.textMain = (TextView) v.findViewById(R.id.item_zx_content);
            holder.midImg1 = (ImageView) v
                    .findViewById(R.id.item_zx_centerimg1);
            holder.midImg2 = (ImageView) v
                    .findViewById(R.id.item_zx_centerimg2);
            holder.midImg3 = (ImageView) v
                    .findViewById(R.id.item_zx_centerimg3);
            holder.leftImg = (ImageView) v.findViewById(R.id.item_zx_leftimg);
            holder.stickyView = (View) v.findViewById(R.id.item_zx_sticky);
            holder.cate = (TextView) v.findViewById(R.id.item_zx_cate);
            holder.comments = (TextView) v.findViewById(R.id.item_zx_cmt);
            holder.eye = (TextView) v.findViewById(R.id.item_zx_eye);
            holder.time = (TextView) v.findViewById(R.id.item_zx_time);
            holder.centerLayout = (View) v
                    .findViewById(R.id.item_zx_center_layout);
            holder.cmtTag = (View) v.findViewById(R.id.item_zx_cmt_tag);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        NewsList item = listData.get(position);
        holder.textMain.setText(item.getTitle());
        holder.cate.setText(item.getCatname());
        holder.comments.setText(item.getCommentNum());
        if (length != -1) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) holder.centerLayout.getLayoutParams();
            lp.height = length;
            holder.centerLayout.setLayoutParams(lp);
        }
        if (item.getScanNum() == null | item.getScanNum() == "") {
            holder.eye.setText("");

        } else {
            holder.eye.setText(item.getScanNum());
        }
        if (item.getCommentNum() == null | item.getCommentNum() == "") {
            holder.cmtTag.setVisibility(View.GONE);
        } else {
            holder.cmtTag.setVisibility(View.VISIBLE);
        }
        if (item.getCreate_time() != null) {
            holder.time.setText(item.getCreate_time());
        }
        if (item.getImgUrl() == null | item.getImgUrl().size() == 0) {
            holder.leftImg.setVisibility(View.GONE);
            holder.centerLayout.setVisibility(View.GONE);
        }
        if (item.getImgUrl().size() == 1 | item.getImgUrl().size() == 2) {
            holder.leftImg.setVisibility(View.VISIBLE);
            holder.centerLayout.setVisibility(View.GONE);
            Glide.with(mContext).load(item.getImgUrl().get(0) + "_300x10.png")
                    .placeholder(R.drawable.ic_menu_rotate)
                    .error(R.drawable.ic_menu_rotate).into(holder.leftImg);
        }
        if (item.getImgUrl().size() == 3) {
            holder.leftImg.setVisibility(View.GONE);
            holder.centerLayout.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(item.getImgUrl().get(0) + "_300x10.png")
                    .placeholder(R.drawable.ic_menu_rotate)
                    .error(R.drawable.ic_menu_rotate).into(holder.midImg1);
            Glide.with(mContext).load(item.getImgUrl().get(1) + "_300x10.png")
                    .placeholder(R.drawable.ic_menu_rotate)
                    .error(R.drawable.ic_menu_rotate).into(holder.midImg2);
            Glide.with(mContext).load(item.getImgUrl().get(2) + "_300x10.png")
                    .placeholder(R.drawable.ic_menu_rotate)
                    .error(R.drawable.ic_menu_rotate).into(holder.midImg3);

        }
        if (item.getTop().equals("1")) {
            holder.stickyView.setVisibility(View.VISIBLE);
        } else {
            holder.stickyView.setVisibility(View.GONE);
        }
        // holder.name.setText(list.get(position).getRealname());
        // holder.worksNum.setText(list.get(position).getArtNum()+"件作品");
        // holder.followNum.setText(list.get(position).getFollowNum()+"人关注");

        return v;
    }

    class ViewHolder {

        ImageView leftImg, midImg1, midImg2, midImg3, imgSticky;
        TextView textMain, time, cate, comments, eye;
        View stickyView,  centerLayout,  cmtTag;

    }

}
